﻿/****************************** Module Header ******************************\

Copyright (c) Gary Davidson

This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System.Linq;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Savviety.ServiceManager.Facade.Criteria;
using Savviety.ServiceManager.Facade.EnterpriseManagementClasses;
using Savviety.ServiceManager.Facade.ManagementPacks;
 

namespace Savviety.ServiceManager.Facade
{
    public static class SCSMChangeRequest
    {

        public static ManagementPackClass ChangeRequestClass = System_WorkItem_ChangeRequestClass.GetManagementPackClass();


        public static EnterpriseManagementObjectProjection CreateChangeRequest ()
        {
            return new EnterpriseManagementObjectProjection(SCSMServer.CurrentManagementGroup, ChangeRequestClass);
        }

        public static EnterpriseManagementObjectProjection GetChangeRequest(string id)
        {

            var builder = new ProjectionCriteriaBuilder();

            builder.AddReferences(ManagementPackNames.System_WorkItem_ChangeRequest_Library, "ChangeRequest");

            const string property = "$Target/Property[Type='ChangeRequest!System.WorkItem.ChangeRequest']/Id$";

            var criteria = builder.CreateSimpleCriteria(TypeProjections.TypeProjections.System_WorkItem_ChangeRequestProjection, property, id);

            return SCSMServer.GetObjectProjectionReader(criteria, ObjectQueryOptions.Default).FirstOrDefault();
        }
        public static EnterpriseManagementObjectProjection GetChangeRequestAndActivities(string id)
        {

            var builder = new ProjectionCriteriaBuilder();

            builder.AddReferences(ManagementPackNames.System_WorkItem_ChangeRequest_Library, "ChangeRequest");

            const string property = "$Target/Property[Type='ChangeRequest!System.WorkItem.ChangeRequest']/Id$";

            var criteria = builder.CreateSimpleCriteria(TypeProjections.TypeProjections.System_WorkItem_ChangeRequestAndActivityViewProjection, property, id);

            return SCSMServer.GetObjectProjectionReader(criteria, ObjectQueryOptions.Default).FirstOrDefault();


        }

    //public static 
    //    IObjectProjectionReader<EnterpriseManagementObject> GetChangeRequestReader
    //    (string
    //    id)
    //{

    //    var builder = new CriteriaBuilder();
    //    builder.AddReferences(SCSMServer.SystemWorkItemChangeRequestLibrary.Name, "ChangeRequest");

    //    const string property = "$Target/Property[Type='ChangeRequest!System.WorkItem.ChangeRequest']/Id$";

    //    var criteria = builder.CreateSimpleCriteria(SCSMServer.CommonTypeProjections.ChangeRequestProjection,
    //                                                property, id);

    //    return SCSMServer.GetObjectProjectionReader(criteria, ObjectQueryOptions.Default);


    //}

    }
}

